package com.dpmc.service;
  
import java.util.HashMap;
import java.util.Map;

import com.common.util.ObjectUtils;
import com.core.service.AppBaseService; 
import com.dpmc.entity.*;  

import javax.ejb.Stateless;

/**
 * 银行卡管理
 */
@Stateless
public class SVC_HYKGL extends AppBaseService implements SVC_HYKGLRemote, SVC_HYKGLLocal {

	@Override
	public Map ADD_JBXX(Map param) { 
		TB_HYKGL_JBXX bo =  ObjectUtils.mapToJavaBean(param, TB_HYKGL_JBXX.class);
		bo.setID(bo.uuid());
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		getJpaDao().save(bo); 
		
		TB_HYKGL_HYXX hyxx =  ObjectUtils.mapToJavaBean(param, TB_HYKGL_HYXX.class);
		hyxx.setID(hyxx.uuid());
		hyxx.setHYKID(bo.getID());
		hyxx.setCREATE_TIME(getOpTime(param));
		hyxx.setOP_ITEM(getOpTime(param));
		hyxx.setOPID(getOpid(param));
		getJpaDao().save(hyxx); 
		
		Map USER=new HashMap();
		if(USER!=null){
			copySysParam(param, USER);
			USER.put("INFO_ID", bo.getID()); 
			USER.put("INFO_TYPE", "TB_HYKGL_JBXX"); 
			USER.put("INFO_NAME", hyxx.getXM());  
			USER.put("USERNAME", bo.getKH());
			USER.put("PASSWORD", bo.getKH().substring(bo.getKH().length()-6));

			getServiceInterface().doService("RMServiceBean", "addSysUser", USER); 
		}  
		
		//商户财务账户
		Map cwzh = new HashMap();
		cwzh.put("ZHLX", "TB_HYKGL_JBXX");
		cwzh.put("GLID", bo.getID()); 
		copySysParam(param,cwzh);
		getServiceInterface().doService("SVC_CWGL", "ADD_ZH", cwzh);
		if(bo.getCSWB()>0){
			double je = bo.getCSWB();
			 getServiceInterface().doService("SVC_CWGL", "RZJY", "TB_HYKGL_JBXX",bo.getID(),"TB_HYKGL_JBXX.CSWB",bo.getID(),getOpTime(param),je);
		}
		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_JBXX(Map param) { 
		TB_HYKGL_JBXX bo =  ObjectUtils.mapToJavaBean(param, TB_HYKGL_JBXX.class); 
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		TB_HYKGL_JBXX po = getJpaDao().find(TB_HYKGL_JBXX.class, bo.getID());
		ObjectUtils.copyJavaBeanToBean(bo, po, "CREATE_TIME");
		getJpaDao().update(po);
		
		TB_HYKGL_HYXX hyxx_bo = ObjectUtils.mapToJavaBean(param, TB_HYKGL_HYXX.class); 
		hyxx_bo.setOP_ITEM(getOpTime(param));
		hyxx_bo.setOPID(getOpid(param));
		Map queryParam = new HashMap();
		queryParam.put("HYKID", bo.getID());
		TB_HYKGL_HYXX hyxx_po = (TB_HYKGL_HYXX) querySingleResult("Q_HYKGL.GET_TB_HYKGL_HYXX_BY_HYKID",queryParam);
		ObjectUtils.copyJavaBeanToBean(hyxx_bo, hyxx_po, "CREATE_TIME","HYKID","ID");
		getJpaDao().update(hyxx_po);
		
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_JBXX(Map param) {
		TB_HYKGL_JBXX dto =  ObjectUtils.mapToJavaBean(param, TB_HYKGL_JBXX.class); 
		TB_HYKGL_JBXX po =getJpaDao().find(TB_HYKGL_JBXX.class, dto.getID()); 
		     
		 po.setOP_ITEM(getOpTime(param));
		 po.setOPID(getOpid(param));
		 po.setSTATUS(TB_SHGL_LXDY.STATUS_DEAD);
		 getJpaDao().update(po);  
	}

	@Override
	public Map ADD_KZXX(Map param) {
		TB_HYKGL_KZXX tdo = ObjectUtils.mapToJavaBean(param, TB_HYKGL_KZXX.class); 
		tdo.setID(tdo.uuid()); 
		tdo.setCREATE_TIME(getOpTime(param));
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));  
		getJpaDao().save(tdo);
		return ObjectUtils.javaBeanToMap(tdo);
	}

	@Override
	public Map UPD_KZXX(Map param) {
		TB_HYKGL_KZXX tdo = ObjectUtils.mapToJavaBean(param, TB_HYKGL_KZXX.class);  
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param)); 
		
		TB_HYKGL_KZXX po = getJpaDao().find(TB_HYKGL_KZXX.class,tdo.getID());   
		
		ObjectUtils.copyJavaBeanToBean(tdo, po, "CREATE_TIME" ); 
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_KZXX(Map param) {
		TB_HYKGL_KZXX tdo = ObjectUtils.mapToJavaBean(param, TB_HYKGL_KZXX.class); 
		
		StringBuffer hql = new StringBuffer("select count(o) from TB_HYKGL_JBXX o where KZID='").append(tdo.getID()).append("'");
		int iRet = queryForResultSize(hql.toString());
		if(iRet>0){
			throw new RuntimeException("存在关联不能删除！");
		}
		
		getJpaDao().delete(TB_HYKGL_KZXX.class,  tdo.getID());
	}

	@Override
	public Map ADD_HYXX(Map param) {
		TB_HYKGL_HYXX tdo = ObjectUtils.mapToJavaBean(param, TB_HYKGL_HYXX.class); 
		tdo.setID(tdo.uuid()); 
		tdo.setCREATE_TIME(getOpTime(param));
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param));  
		getJpaDao().save(tdo);
		return ObjectUtils.javaBeanToMap(tdo);
	}

	@Override
	public Map UPD_HYXX(Map param) {
		TB_HYKGL_HYXX tdo = ObjectUtils.mapToJavaBean(param, TB_HYKGL_HYXX.class);  
		tdo.setOP_ITEM(getOpTime(param));
		tdo.setOPID(getOpid(param)); 
		
		TB_HYKGL_HYXX po = getJpaDao().find(TB_HYKGL_HYXX.class,tdo.getID());   
		
		ObjectUtils.copyJavaBeanToBean(tdo, po, "CREATE_TIME" ); 
		getJpaDao().update(po);
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_HYXX(Map param) {
		TB_HYKGL_HYXX dto =  ObjectUtils.mapToJavaBean(param, TB_HYKGL_HYXX.class); 
		TB_HYKGL_HYXX po =getJpaDao().find(TB_HYKGL_HYXX.class, dto.getID()); 
		     
		 po.setOP_ITEM(getOpTime(param));
		 po.setOPID(getOpid(param));
		 po.setSTATUS(TB_SHGL_LXDY.STATUS_DEAD);
		 getJpaDao().update(po);  
		
	}

	@Override
	public Map ADD_FKQDS(Map param) {
		TB_HYKGL_FKQDS bo = ObjectUtils.mapToJavaBean(param, TB_HYKGL_FKQDS.class);
		bo.setID(bo.uuid());
		bo.setCREATE_TIME(getOpTime(param));
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		getJpaDao().save(bo);
		
		
		Map yhzh=(Map)param.get("OBJECT_TB_YHZH");
		if(yhzh!=null){
			TB_YHZH yhzh_vo =  ObjectUtils.mapToJavaBean(yhzh, TB_YHZH.class);
			yhzh_vo.setID(yhzh_vo.uuid());
			yhzh_vo.setGLLX("TB_HYKGL_FKQDS");
			yhzh_vo.setGLID(bo.getID());
			yhzh_vo.setCREATE_TIME(getOpTime(param));
			yhzh_vo.setOP_ITEM(getOpTime(param));
			yhzh_vo.setOPID(getOpid(param) );  
			getJpaDao().save(yhzh_vo);
		} 
		
		//商户财务账户
	    Map cwzh = new HashMap();
		cwzh.put("ZHLX", "TB_HYKGL_FKQDS");
		cwzh.put("GLID", bo.getID()); 
		copySysParam(param,cwzh);
		getServiceInterface().doService("SVC_CWGL", "ADD_ZH", cwzh); 
		
		
		return ObjectUtils.javaBeanToMap(bo);
	}

	@Override
	public Map UPD_FKQDS(Map param) {
		TB_HYKGL_FKQDS bo = ObjectUtils.mapToJavaBean(param, TB_HYKGL_FKQDS.class);
		bo.setOP_ITEM(getOpTime(param));
		bo.setOPID(getOpid(param));
		TB_HYKGL_FKQDS po = getJpaDao().find(TB_HYKGL_FKQDS.class, bo.getID());
		ObjectUtils.copyJavaBeanToBean(bo, po, "CREATE_TIME");
		getJpaDao().update(po);
		
		Map yhzh=(Map)param.get("OBJECT_TB_YHZH");
		if(yhzh!=null){
			TB_YHZH yhzh_vo =  ObjectUtils.mapToJavaBean(yhzh, TB_YHZH.class);
			yhzh_vo.setOP_ITEM(getOpTime(param));
			yhzh_vo.setOPID(getOpid(param) ); 
			if(yhzh_vo.getID()==null){
				yhzh_vo.setID(yhzh_vo.uuid());
				yhzh_vo.setGLLX("TB_HYKGL_FKQDS");
				yhzh_vo.setGLID(bo.getID());
				yhzh_vo.setCREATE_TIME(getOpTime(param)); 
				getJpaDao().save(yhzh_vo);
			} else{
				TB_YHZH yhzh_po =getJpaDao().find(TB_YHZH.class, yhzh_vo.getID());  
				ObjectUtils.copyJavaBeanToBean(yhzh_vo, yhzh_po, "ID","CREATE_TIME","GLLX","GLID"); 
				getJpaDao().update(yhzh_po);
			}
		} 
		 
		return ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_FKQDS(Map param) {
		TB_HYKGL_FKQDS dto = ObjectUtils.mapToJavaBean(param, TB_HYKGL_FKQDS.class);
		TB_HYKGL_FKQDS po = getJpaDao().find(TB_HYKGL_FKQDS.class, dto.getID());

		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_SHGL_LXDY.STATUS_DEAD);
		getJpaDao().update(po);
	}
       
    

}
